﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public float x_d = 2f;
    public float y_d = 2f;
    public float x_smooth = 2f;
    public float y_smooth = 2f;
    public Vector2 minPos, maxPos;
    public Transform playerTrans;

    void FixedUpdate()
    {
        trackPlayer();
    }

    bool checkXD()
    {
        return Mathf.Abs(playerTrans.position.x - transform.position.x) > x_d;
    }

    bool checkYD()
    {
        return Mathf.Abs(playerTrans.position.y - transform.position.y) > y_d;
    }
    
    void trackPlayer()
    {
        float newX = transform.position.x, newY = transform.position.y;
        if (checkXD() || checkYD())
        {
            newX = Mathf.Lerp(transform.position.x, playerTrans.position.x, x_smooth * Time.deltaTime);
            newY = Mathf.Lerp(transform.position.y, playerTrans.position.y, y_smooth * Time.deltaTime);
        }
        newX = Mathf.Clamp(newX, minPos.x, maxPos.x);
        newY = Mathf.Clamp(newY, minPos.y, maxPos.y);
        transform.position = new Vector3(newX, newY, transform.position.z);
    }
   
}
